/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.service;

import com.stip.server.da.AdminDA;
import com.stip.server.da.OrgDA;
import com.stip.server.da.UserMainDA;
import com.stip.server.da.UserTokenDA;
import com.stip.server.entities.Admin;
import com.stip.server.entities.Organization;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
@Path("org")
public class OrgService {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 @EJB   
 OrgDA orgDA;
 @EJB
 UserTokenDA userTokenDA;
 @EJB
 AdminDA adminDA;
 @EJB
 UserMainDA userMainDA;
    
 //获取组织信息
 @GET
 @Path("/{id}")
 @Produces(MediaType.APPLICATION_JSON)
 public Organization getOrganization(@PathParam("id") Integer id){
     if ( orgDA.orgFindByID(id)!=null) {
         return   orgDA.orgFindByID(id);
     } else {
         throw  new WebApplicationException(404);
     }
 
 
 }
 
 //需要复查
 //增加组织
 @POST
 @Produces(MediaType.APPLICATION_JSON)
  public Organization postOrganization(@QueryParam("token") String token,@QueryParam("name")String org_name,@QueryParam("email")String email,@QueryParam("qq")String qq,@QueryParam("phone")String phone,@QueryParam("abstracts")String abstracts){
     if (userTokenDA.findToken(token)!=null) {
         String checkID=userTokenDA.findToken(token);
         Integer orgID= orgDA.createOrg(org_name, email, qq, phone, abstracts);
         
         Integer adminID =adminDA.addAdmin(orgID,checkID);
         if (adminID!=0) {
               return  orgDA.orgFindByID(orgID);
         } else {
             orgDA.deleteOrg(orgID);
             throw  new WebApplicationException(400);
         }
      
     } else {
         throw  new WebApplicationException(401);
     }
     
 }
//获取管理员名单
 @GET
 @Path("/{id}/admins")
 @Produces(MediaType.APPLICATION_JSON)
 public  List<Admin> getAdmins(@PathParam("id") Integer id){
     Organization organization=orgDA.orgFindByID(id);
     if (organization!=null) {
         return  organization.getAdminList();
     } else {
         throw  new WebApplicationException(404);
     }
 
 }
 
 //半睡眠需要复查
 //增加管理员
 @POST
 @Path("/{id}/admin")
 @Produces(MediaType.APPLICATION_JSON)
 public Boolean postAdmin(@PathParam("id") Integer id,@QueryParam("auid") String auid,@QueryParam("token") String token ){
     if (userTokenDA.findToken(token)!=null) {
         
         if (orgDA.orgFindByID(id)!=null) {
            String checkID=userTokenDA.findToken(token);
            List<Admin> admins=orgDA.orgFindByID(id).getAdminList();
             for (Iterator<Admin> it = admins.iterator(); it.hasNext();) {
                 Admin admin = it.next();
             List<Admin> admins1=     userMainDA.findUsersMainById(checkID).getAdminList();
                 if (admins1.contains(admin)) {
                     if ( adminDA.addAdmin(id,auid)!=0) {
                         return  true;
                     } else {
                         throw  new WebApplicationException(400);
                     }
                   
                 } 
                
                 
             }
            throw  new WebApplicationException(401);
         } else {
             throw  new WebApplicationException(404);
         }
     } else {
        throw  new WebApplicationException(401);
     }
 }

 //删除管理员
 @DELETE
 @Path("/{id}/admin/{aid}")
 @Produces(MediaType.APPLICATION_JSON)
 public Boolean deleteAdmin(@PathParam("id") Integer id,@PathParam("aid") Integer aid,@QueryParam("token") String token){
     if (userTokenDA.findToken(token)!=null) {
         if (adminDA.findAdmin(aid)!=null) {
           String checkID=userTokenDA.findToken(token);
           List<Admin> admins=  orgDA.orgFindByID(id).getAdminList();
             for (Iterator<Admin> it = admins.iterator(); it.hasNext();) {
                 Admin admin = it.next();
                 List<Admin> admins1=userMainDA.findUsersMainById(checkID).getAdminList();
                 if (admins1.contains(admin)) {
                     if (adminDA.deleteAdmin(aid)) {
                         return  true;
                     }
                     else{
                     throw  new WebApplicationException(400);
                     }
                 }
                
             }
             throw  new WebApplicationException(401);
         } else {
             throw  new WebApplicationException(404);
         }
     } else {
         throw new WebApplicationException(401);
     }
 
 }
 //更改组织信息
 @PUT
 @Path("/{id}")
 @Produces(MediaType.APPLICATION_JSON)
 public Organization putOrganization(@PathParam("id") Integer id,  @QueryParam("token") String token,@QueryParam("name")String org_name,@QueryParam("email")String email,@QueryParam("qq")String qq,@QueryParam("phone")String phone,@QueryParam("abstracts")String abstracts){
     if (userTokenDA.findToken(token)!=null) {
                 if (orgDA.orgFindByID(id)!=null) {
            String checkID=userTokenDA.findToken(token);
            List<Admin> admins=orgDA.orgFindByID(id).getAdminList();
             for (Iterator<Admin> it = admins.iterator(); it.hasNext();) {
                 Admin admin = it.next();
             List<Admin> admins1=     userMainDA.findUsersMainById(checkID).getAdminList();
                 if (admins1.contains(admin)) {
                     if (orgDA.editOrg(id, org_name, email, qq, phone, abstracts)) {
                         return  orgDA.orgFindByID(id);
                     } else {
                         throw  new WebApplicationException(400);
                     }
                   
                 } 
                
                 
             }
            throw  new WebApplicationException(401);
         } else {
             throw  new WebApplicationException(404);
         }
         
     } else {
         throw  new WebApplicationException(401);
     }
     
 }
//删除组织
 @DELETE
 @Path("/{id}")
 @Produces(MediaType.APPLICATION_JSON)
 public Boolean deleteOrg(@QueryParam("token") String token,@PathParam("id") Integer id ){
      if (userTokenDA.findToken(token)!=null) {
                 if (orgDA.orgFindByID(id)!=null) {
            String checkID=userTokenDA.findToken(token);
            List<Admin> admins=orgDA.orgFindByID(id).getAdminList();
             for (Iterator<Admin> it = admins.iterator(); it.hasNext();) {
                 Admin admin = it.next();
             List<Admin> admins1=     userMainDA.findUsersMainById(checkID).getAdminList();
                 if (admins1.contains(admin)) {
                     if (orgDA.deleteOrg(id)) {
                         return  true;
                     } else {
                         throw  new WebApplicationException(400);
                     }
                   
                 } 
                
                 
             }
            throw  new WebApplicationException(401);
         } else {
             throw  new WebApplicationException(404);
         }
         
     } else {
         throw  new WebApplicationException(401);
     }
 }
}
